var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    current_page: 1,
    last_page: 1,
    aid:'',
    show_type:0,
    axis:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split("&");
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          if (chil_arr[0] == "m"){
            app._setMid(chil_arr[1])
          }else if (chil_arr[0] == "id") {
            this.setData({
              aid: chil_arr[1],
            });
          }
        }
      }
    } 
    if(options.id){
      this.setData({aid:options.id});
    }
    this.getData();
  },
  change(e){
  
    this.setData({show_type:e.detail})
    app._getNetWork({
      url:app.getNetAddresss("plugin.time-axis.frontend.controllers.axis.eventList"),
      data:{
        show_type:this.data.show_type,
        aid:this.data.aid
      },
      success:resdata=>{
        let data = resdata.data.data;
        if(resdata.data.result == 0){
          return app.tips(resdata.data.msg);
        }
        data.list.data.forEach((item) => {
          if (item.label_arr && item.label_arr.length !== 0) {
              item.label_color = this.createGroups(item.label_arr, 4);
              item.label_color=  this.feat_color(item.label_color)
          }
        })
        this.setData({
          list:data.list.data,
          current_page:data.list.current_page,
          last_page:data.list.last_page
        })
      }
    })
 

  },
  link(e){
    let data = e.currentTarget.dataset.datas;
    if(data.mini_link_type==1){
      if(data.mini_link_path){
        wx.navigateTo({
          url: `${data.mini_link_path}`,
        })
      }
    }else if(data.mini_link_type==2){
      if(data.out_mini_link_path && data.out_mini_app_id){
        wx.navigateToMiniProgram({
          appId:data.out_mini_app_id,
          path:data.out_mini_link_path
        })
      }
    }
 
  },
  getData(){
    app._getNetWork({
      url:app.getNetAddresss("plugin.time-axis.frontend.controllers.axis.eventList"),
      data:{
        aid:this.data.aid
      },
      success:resdata=>{
        let data = resdata.data.data;
        if(resdata.data.result == 0){
          return app.tips(resdata.data.msg);
        }
        data.list.data.forEach((item) => {
          if (item.label_arr && item.label_arr.length !== 0) {
              item.label_color = this.createGroups(item.label_arr, 4);
              item.label_color=  this.feat_color(item.label_color)
          }
        })
        this.setData({
          axis:data.axis,
          show_type:data.axis.show_type,
          list:data.list.data,
          current_page:data.list.current_page,
          last_page:data.list.last_page
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.current_page>=this.data.last_page){
      return;
    }
    let page = this.data.current_page + 1;

      app._getNetWork({
        url:app.getNetAddresss("plugin.time-axis.frontend.controllers.axis.eventList"),
        data:{page,aid:this.data.aid,show_type:this.data.show_type},
      success:resdata=>{
        let data = resdata.data.data;
        if(resdata.data.result == 0){
          return app.tips(resdata.data.msg);
        }
 
        
        data.list.data.forEach((item) => {
          if (item.label_arr && item.label_arr.length !== 0) {
              item.label_color = this.createGroups(item.label_arr, 4);
              item.label_color=  this.feat_color(item.label_color)
          }
        })
        
        this.setData({
          list:this.data.list.concat(data.list.data),
          current_page:data.list.current_page,
          last_page:data.list.last_page
        })
      }
    })
  },
  createGroups(array, subGroupLength) {
    let index = 0;
    let newArray = [];
    while (index < array.length) {
      newArray.push(array.slice(index, index += subGroupLength));
    }
    return newArray;
  },
  feat_color(arr){
    let _newArr = []
    let _color = ['#13C7A7', '#FCA814', '#FC6614', '#1481FC']
    arr.forEach((item,i) => {
        _newArr[i] =[]
      item.forEach((c,c_i)=>{
        _newArr[i].push({color:_color[c_i],label:c})
      })
    })
    let _result = []
    _newArr.forEach((item)=>{
      item.forEach(c=>{
        _result.push(c)
      })
    })
    return _result;
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})